package Service;

import GUI.ChatFrame;
import Monitor.ClientThread;
import Utils.ChatManage;
import Utils.Date;
import Utils.ThreadManage;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import static Client.Client.rootDir;

/**
 * The type Receive file.
 *
 * @author dzr
 * @version 1.0
 */
public class ReceiveFile {
    /**
     * The constant MSG_IMG.
     */
    public static final int MSG_IMG = 1;
    /**
     * The constant MSG_FILE.
     */
    public static final int MSG_FILE = 2;

    /**
     * Receive file.
     *
     * @param jsonObject the json object
     */
    public static void receiveFile(JSONObject jsonObject){
        int s_uid = jsonObject.getInt("s_uid");
        String s_name = jsonObject.getString("s_name");
        int r_uid = jsonObject.getInt("r_uid");
        int fileType = jsonObject.getInt("fileType");
        String fileName = jsonObject.getString("fileName");
        long fileLength = jsonObject.getLong("fileLength");
        ClientThread clientThread = ThreadManage.getInstance().getClientThreads().get(r_uid);
        File dest;
        if(fileType == MSG_IMG){
            dest = new File(clientThread.getRootImage().getPath() + File.separator + fileName);
        }else{
            dest = new File(clientThread.getFileRecv().getPath() + File.separator + fileName);
        }
        DatagramSocket socket = clientThread.getSocket();
        Runnable receiveThread = new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] buffer = new byte[1024];
                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                    FileOutputStream output = new FileOutputStream(dest);
                    int curLen;
                    int totalLen = 0;
                    while (totalLen < fileLength) {
                        socket.receive(packet);
                        curLen = packet.getLength();
                        if (curLen + totalLen < fileLength) {
                            output.write(buffer,0, curLen);
                            output.flush();
                            totalLen += curLen;
                        }else{
                            int rLength = (int) (fileLength - totalLen);
                            output.write(buffer,0, rLength);
                            output.flush();
                            totalLen += rLength;
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        receiveThread.run();
        String time = Date.getCurTime();
        if(ChatManage.hasChat(s_uid)){
            ChatManage.getChat(s_uid).showMsg(fileType, s_uid, s_name, time, dest.getPath());
        }else{
            ChatFrame chatFrame = new ChatFrame(ThreadManage.getInstance().getClientThread(r_uid).getUsername(), r_uid, s_name, s_uid);
            ChatManage.addChat(s_uid, chatFrame);
            chatFrame.showMsg(fileType, s_uid, s_name, time, dest.getPath());
        }
    }
}
